#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "def.h"
#include "stu.h"

int main() {
    LIST lists;
    lists.length = 0;
    char name[20];
    BiTree T, p;
    T = NULL;
    int op, select, i, LR, j;
    KeyType e;
    TElemType value;
    printf("请输入您的选择 1 代表对多表操作 2 代表对二叉树进行操作 0 代表退出\n");
    scanf("%d", &select);
    while (select) {
        switch (select) {
        case 1:
            help_list();
            printf("请输入你的选择\n");
            scanf("%d", &op);
            while (op) {
                switch (op) {
                case 1:
                    printf("请输入二叉树的名字\n");
                    scanf("%s", name);
                    i = createlist(lists, name);
                    switch (i) {
                    case OVERFLOW:
                        printf("列表已满无法创建\n");
                        break;
                    case OK:
                        printf("创建成功\n");
                        break;
                    }
                    break;
                case 2:
                    printf("请输入二叉树的名字");
                    scanf("%s", name);
                    i = deletelist(lists, name);
                    switch (i) {
                    case INFEASIBLE:
                        printf("多表为空\n");
                        break;
                    case OK:
                        printf("删除成功\n");
                        break;
                    }
                    break;
                case 3:
                    printf("请输入二叉树的名字\n");
                    scanf("%s", name);
                    i = checklits(lists, name);
                    switch (i) {
                    case ERROR:
                        printf("多表为空\n");
                        break;
                    case -1:
                        printf("未查询到这个多表\n");
                        break;
                    default:
                        printf("您要查询的 %s 的位置是 %d \n", name, i);
                        break;
                    }
                    break;
                case 4:
                    help_list();
                    break;
                default:
                    printf("您的选择有误请重新选择\n");
                    break;
                }
                printf("请输入你的选择\n");
                scanf("%d", &op);
            }
            break;
        case 2:
            printf("请输入您要操作的二叉树名字\n");
            scanf("%s", name);
            i = checklits(lists, name);
            if (i > 0)
                T = lists.elem[i - 1].T;
            else {
                printf("未查询到您要查询的二叉树\n");
                break;
            }
            help_tree();
            printf("请输入您的选择\n");
            scanf("%d", &op);
            while (op) {
                switch (op) {
                case 1:
                    if (T != NULL) {
                        printf("二叉树已存在\n");
                        break;
                    } else {
                        printf("请输入数据用0 NULL表示空结点 -1 NULL 表示结尾\n");
                        TElemType definition[100];
                        j = 0;
                        do {
                            scanf("%d%s", &definition[j].key, definition[j].others);
                        } while (definition[j++].key != -1);
                        ii = 0;
                        i = CreateBiTree(T, definition);
                        switch (i) {
                        case INFEASIBLE:
                            printf("数据无效\n");
                            break;
                        case OK:
                            printf("成功\n");
                            break;
                        }
                    }
                    break;
                case 2:
                    i = ClearBiTree(T);
                    switch (i) {
                    case OK:
                        printf("成功\n");
                        break;
                    }
                    break;
                case 3:
                    i = BiTreeDepth(T);
                    printf("此二叉树的深度是%d\n", i);
                    break;
                case 4:
                    printf("请输入您要查询的结点的关键字\n");
                    scanf("%d", &e);
                    p = LocateNode(T, e);
                    if (p)
                        printf("%d,%s\n", p->data.key, p->data.others);
                    else
                        printf("查找失败");
                    break;
                case 5:
                    printf("请输入您要赋值的结点关键字及结点数据\n");
                    scanf("%d", &e);
                    scanf("%d%s", &value.key, value.others);
                    i = Assign(T, e, value);
                    if (i == 0)
                        printf("赋值操作失败\n");
                    else
                        printf("成功\n");
                    break;
                case 6:
                    scanf("%d", &e);
                    p = GetSibling(T, e);
                    if (p)
                        printf("%d,%s", p->data.key, p->data.others);
                    else
                        printf("无兄弟结点");
                    break;

                case 7:
                    printf("请输入结点关键字 插入类型(-1,0,1) 以及结点数据\n");
                    scanf("%d", &e);
                    scanf("%d", &LR);
                    scanf("%d%s", &value.key, value.others);
                    i = InsertNode(T, e, LR, value);
                    switch (i) {
                    case ERROR:
                        printf("插入失败\n");
                        break;
                    case OK:
                        printf("成功\n");
                        break;
                    }
                    break;
                case 8:
                    printf("请输入结点关键字\n");
                    scanf("%d", &e);
                    i = DeleteNode(T, e);
                    if (i == 0)
                        printf("删除失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 9:
                    i = PreOrderTraverse(T, visit);
                    if (i == INFEASIBLE)
                        printf("先序遍历失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 10:
                    i = InOrderTraverse(T, visit);
                    if (i == INFEASIBLE)
                        printf("中序遍历失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 11:
                    i = PostOrderTraverse(T, visit);
                    if (i == INFEASIBLE)
                        printf("后序遍历失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 12:
                    i = LevelOrderTraverse(T, visit);
                    if (i == INFEASIBLE)
                        printf("按层遍历失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 13:
                    printf("请输入文件的名字\n");
                    scanf("%s", name);
                    i = SaveBiTree(T, name);
                    if (i == INFEASIBLE)
                        printf("二叉树为空\n");
                    else if (i == ERROR)
                        printf("文件打开失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 14:
                    printf("请输入文件名字\n");
                    scanf("%s", name);
                    i = LoadBiTree(T, name);
                    if (i == ERROR)
                        printf("文件打开失败\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 15:
                    i = MaxPathSum(T);
                    printf("此二叉树的最大路径和为 %d\n", i);
                    break;
                case 16:
                    i = InvertTree(T);
                    if (i == ERROR)
                        printf("此二叉树为空\n");
                    else if (i == OK)
                        printf("成功\n");
                    break;
                case 17:
                    help_tree();
                    break;
                case 18:
                    i = BiTreeEmpty(T);
                    if (i == TRUE)
                        printf("二叉树为空\n");
                    else if (i == FALSE)
                        printf("二叉树不为空\n");
                    break;
                default:
                    printf("您输入的选择有误请重新选择\n");
                    break;
                }
                printf("请输入你的选择\n");
                scanf("%d", &op);
            }
        }
        printf("请输入您的选择 1 代表对多表操作 2 代表对二叉树进行操作 0 代表退出\n");
        scanf("%d", &select);
    }
    printf("感谢您的使用\n");
    return 1;
}
